package com.dfs.parent.sentinel.config;

import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.dfs.parent.common.core.domain.Result;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;

import javax.servlet.http.HttpServletRequest;

/**
 * Sentinel配置类
 *
 */
@AutoConfiguration
@ConditionalOnClass(HttpServletRequest.class)
public class SentinelConfig {
    @Bean
    public BlockExceptionHandler webmvcBlockExceptionHandler() {
        return new SentinelMVCBlockExceptionHandler();
    }
    /**
     * 限流、熔断统一处理类
     */
//    @Configuration
//    @ConditionalOnClass(HttpServletRequest.class)
//    public static class WebmvcHandler {
//        @Bean
//        public BlockExceptionHandler webmvcBlockExceptionHandler() {
//            return new SentinelMVCBlockExceptionHandler();
//        }
//    }


    /**
     * 限流、熔断统一处理类
     */
//    @Configuration
//    @ConditionalOnClass(ServerResponse.class)
//    public static class WebfluxHandler {
//        @Bean
//        public BlockRequestHandler webfluxBlockExceptionHandler() {
//            return (exchange, t) ->
//                    ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
//                            .contentType(MediaType.APPLICATION_JSON)
//                            .body(BodyInserters.fromValue(Result.fail(t.getMessage())));
//        }
//    }
}
